package com.zry.study.update

import android.app.Notification
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.IBinder
import android.support.v4.app.NotificationCompat

import com.zry.study.MainActivity
import com.zry.study.R
import com.zry.study.config.MyApplication

class UpdateAppService : Service() {
	
	private var myApplication: MyApplication? = null
	private var mNotification: Notification? = null
	private var id = 1
	private var mNotifyManager: NotificationManager? = null
	private var mBuilder: NotificationCompat.Builder? = null
	
	override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
		myApplication = MyApplication()
//		notificationInit()
		
		val updataUtils = UpdateUtils(this, myApplication, mBuilder, mNotifyManager)
		updataUtils.showDownloadAPK("https://www.kangyineng.com/service/downloadApp/")
		
		return super.onStartCommand(intent, flags, startId)
	}
	
	
	
	
	private fun notificationInit() {
		//通知栏内显示下载进度条
		val intent = Intent(this, MainActivity::class.java)//点击进度条，进入程序
		val pIntent = PendingIntent.getActivity(this, 0, intent, 0)
		mNotification = Notification()
		mNotification?.contentIntent = pIntent
		
		mNotifyManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
		mBuilder = NotificationCompat.Builder(this)
		mBuilder!!.setContentTitle("康易能下载").setContentText("下载中，请稍等").setSmallIcon(R.mipmap.ic_launcher)
		
		
		//开始时候，初始化进度条
		mBuilder!!.setProgress(100, 0, false);
		mNotifyManager!!.notify(id, mBuilder!!.build());
	}
	
	
	override fun onBind(intent: Intent): IBinder? {
		return null
	}
}
